package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerPagerAdapter;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.AccountDetailCoinController;
import com.qire.manhua.dal.service.AccountDetailCoinService;
import com.qire.manhua.databinding.LayoutAccountDetailCoinRecordItemBinding;
import com.qire.manhua.databinding.LayoutAccountDetailCoinPagerItemBinding;
import com.qire.manhua.model.enumeration.DetailType;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

public class AccountDetailCoinViewModel extends SmartRefreshViewModel implements IViewModel {

    private AccountDetailCoinService detailService = new AccountDetailCoinService(this);

    private final ArrayList<DetailPagerItemViewModel> detailPagerList = new ArrayList<>();
    private final SummerPagerAdapter detailPagerAdapter = new SummerPagerAdapter(detailPagerList);

    private DetailPagerItemViewModel currentDetailPager;

    public AccountDetailCoinViewModel() {
        refreshBalanceUI();
        refreshDetailTitleUI(0);
        currentDetailPager = new DetailPagerItemViewModel(DetailType.EXPENDITURE);
        detailPagerList.add(currentDetailPager);
        detailPagerList.add(new DetailPagerItemViewModel(DetailType.INCOME));
    }

    @Override
    public int getVariableId() {
        return BR.accountDetailCoinViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     * @param controller 事件控制器
     */
    public void onAddition(AccountDetailCoinController controller) {
//        replyCommentAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        this.detailService = null;
//        this.replyCommentAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin

    public void nextPage() {
        if(ObjectUtil.nonNull(currentDetailPager)) {
            currentDetailPager.nextPage();
        }
    }

    public void changeDetailPagerPosition(int position) {
        // 切换会触发2次，可能导致刷新脏数据，在这里处理是避免2次请求，最好能在API返回处做一个去重处理。
        DetailPagerItemViewModel oldDetailPager = currentDetailPager;
        currentDetailPager = detailPagerList.get(position);
        if(!currentDetailPager.equals(oldDetailPager)) {
            refreshDetailTitleUI(position);
            if (currentDetailPager.pageNo == 1) {
                loadData();
            }
        }
    }

    public void loadData() {
        if(ObjectUtil.nonNull(currentDetailPager)) {
            currentDetailPager.resetPage();
            loadMoreData();
        }
    }
    public void loadMoreData() {
        if(ObjectUtil.nonNull(currentDetailPager)) {
            detailService.fetchCoinDetailRecordBy(currentDetailPager.detailType, currentDetailPager.pageNo);
        }
    }

    public void addDetailRecordItem(CharSequence title, CharSequence description, CharSequence recordTime, CharSequence coinValue) {
        if(ObjectUtil.nonNull(currentDetailPager)) {
            currentDetailPager.addDetailRecordItem(title, description, recordTime, coinValue);
        }
    }
    public void refreshDetailRecordAdapter() {
        if(ObjectUtil.nonNull(currentDetailPager)) {
            currentDetailPager.refreshDetailRecordAdapter();
        }
    }

    //endregion #业务数据 end

    //region    #UI操作 begin

    public void refreshBalanceUI() {
        set("availableBalance", String.valueOf(UserEntity.self.getCoinBalance()));
    }

    public void refreshDetailTitleUI(int position) {
        Integer[] detailTitleSizes = {
                position == 0 ? 16 : 15,
                position == 1 ? 16 : 15
        };
        String[] detailTitleColors = {
                position == 0 ? "#131313" : "#999999",
                position == 1 ? "#131313" : "#999999"
        };
        set("detailTitleSizes", detailTitleSizes);
        set("detailTitleColors", detailTitleColors);
    }

    //endregion #UI操作 end

    //region    #UI绑定 begin

    public MutableLiveData<CharSequence> getAvailableBalance() {
        return get("availableBalance");
    }

    public MutableLiveData<Integer[]> getDetailTitleSizes() {
        return get("detailTitleSizes");
    }
    public MutableLiveData<String[]> getDetailTitleColors() {
        return get("detailTitleColors");
    }

    public PagerAdapter getDetailPagerAdapter() {
        return detailPagerAdapter;
    }

    //endregion #UI绑定 end
    @AdapterEntityBind(viewRid = R.layout.layout_account_detail_coin_pager_item)
    public class DetailPagerItemViewModel extends BaseObservable implements AutoVariable<LayoutAccountDetailCoinPagerItemBinding> {

        private final ArrayList<DetailRecordItemViewModel> detailRecordList = new ArrayList<>();
        private final SummerRecyclerBindAdapter detailRecordAdapter = new SummerRecyclerBindAdapter(detailRecordList);

        private final DetailType detailType;
        private int pageNo = 1;

        public DetailPagerItemViewModel(DetailType detailType) {
            this.detailType = detailType;
        }

        @Override
        public void bindVariable(LayoutAccountDetailCoinPagerItemBinding viewBind) {
            viewBind.setDetailPagerItemViewModel(this);
        }

        private void resetPage() {
            pageNo = 1;
            clearDetailRecordList();
        }

        private void nextPage() {
            pageNo++;
        }

        private void clearDetailRecordList() {
            detailRecordAdapter.notifyItemRangeRemoved(0, detailRecordList.size());
            detailRecordList.clear();
        }
        public void addDetailRecordItem(CharSequence title, CharSequence description, CharSequence recordTime, CharSequence coinValue) {
            detailRecordList.add(new DetailRecordItemViewModel(title, description, recordTime, coinValue, detailType));
        }
        public void refreshDetailRecordAdapter() {
            detailRecordAdapter.notifyItemChanged(0, "full");
        }


        public RecyclerView.Adapter getDetailRecordAdapter() {
            return detailRecordAdapter;
        }

        public RecyclerView.LayoutManager getDetailRecordLayoutManager() {
            return new LinearLayoutManager(null);
        }

    }

    @AdapterEntityBind(viewRid = R.layout.layout_account_detail_coin_record_item)
    public class DetailRecordItemViewModel extends BaseObservable implements AutoVariable<LayoutAccountDetailCoinRecordItemBinding> {

        public final CharSequence title;
        public final CharSequence description;
        public final CharSequence recordTime;
        public final CharSequence coinValue;
        public final DetailType detailType;

        public DetailRecordItemViewModel(CharSequence title, CharSequence description, CharSequence recordTime, CharSequence coinValue, DetailType detailType) {
            this.title = title;
            this.description = description;
            this.recordTime = recordTime;
            this.coinValue = coinValue;
            this.detailType = detailType;
        }


        @Override
        public void bindVariable(LayoutAccountDetailCoinRecordItemBinding viewBind) {
            viewBind.setDetailRecordItemViewModel(this);
        }

        public int getDescriptionVisible() {
            return StringUtil.isEmpty(description) ? View.GONE : View.VISIBLE;
        }

        public int getRecordTimeVisible() {
            return StringUtil.isEmpty(recordTime) ? View.GONE : View.VISIBLE;
        }

        public String getCoinValueColor() {
            return detailType == DetailType.INCOME ? "#19a554" : "#fa5151";
        }

    }

}
